package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCommentMapper;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsCommentServiceImpl implements DtsCommentService {

    @Autowired
    DtsCommentMapper commentMapper;

    @Override
    public IPage<DtsComment> findPage(Integer page, Integer limit, Integer userId,
                                      Integer valueId, String sort, String order, List<Integer> brandIds) {

        IPage<DtsComment> ipage = new Page<>(page, limit);

        QueryWrapper<DtsComment> wrapper = new QueryWrapper<>();

        if (userId != null) {
            wrapper.eq("c.user_id", userId);
        }

        if (valueId != null) {
            wrapper.eq("c.value_id", valueId);
        }

        if (brandIds != null && brandIds.size() > 0) {
            wrapper.in("g.brand_id", brandIds);
        }

        if (order.equalsIgnoreCase("asc")) {
            wrapper.orderByAsc("c." + sort);
        } else {
            wrapper.orderByDesc("c." + sort);
        }

        ipage = commentMapper.findPage(ipage, wrapper);

        return ipage;
    }

    @Override
    public void deleteById(Integer id) {
        commentMapper.deleteById(id);
    }
}
